* set command directory appropriate for your machine
cd "C:\Users\ecoleman\OneDrive - Florida State University\Uganda Extractives\academic papers\big paper\pnas 2019\Data replication"

* results generated by this do file are stored in a folder called "figures"
* that this do file creates

mata: mata clear
set more off

clear
set scheme vg_palec

use "PROJECT DATA.dta"

capture mkdir figures, public
capture mkdir figures\SI, public

capture mkdir temp, public
capture mkdir temp\SI, public

/* install schemes and functions, if neccesary
net install vgsg2, from(http://www.stata-press.com/data/vgsg2) */
*install graph combine if needed
*findit grc1leg
*install tabout
*ssc install tabout
*install sepscatter
*ssc inst sepscatter
*findit svmat2

********************************
******** creating outcomes *****
********************************

*** creating transparency index

recode infosatisfy (3 4=.)
recode aware (4=.)
recode getinfoconf (4=.)
recode infoeffort (4=.)
recode transparency (4 5=.)

gen tftransp = ((tfdirectcomm==1) + ///
			   (tfscholarship==1) + ///
			   (tfrefinary==1) + ///
			   (tfstadium==0) + ///
			   (tfcompensation==1) + ///
			   (tftotal==1) + ///
			   (tfjobs==0) + ///
			   (tflandtake==1) + ///
			   (tfgrievance==0))/9
replace tftransp=. if tfdirectcomm==. & ///
					tfscholarship==. & ///
					tfrefinary==. & ///
					tfstadium==. & ///
					tfcompensation==. & ///
					tftotal==. & ///
					tfjobs==. & ///
					tflandtake==. & ///
					tfgrievance==.
label variable  tftransp "Prop Correct T/F Transparency Questions"

global trans infosatisfy aware getinfoconf infoeffort transparency

	foreach y in $trans {
		qui sum `y'
		gen `y'_r = (`y' - `r(min)') / (`r(max)'-`r(min)')
	}


factor tftransp infosatisfy_r aware_r getinfoconf_r infoeffort_r transparency_r, pcf
rotate
predict pcaindex_trans

*** Political Activity ***

replace hhaction = . if hhaction == 3
replace hhaction = . if hhaction == 4
replace commaction = . if commaction == 3
replace commaction = . if commaction == 4

gen hhoilsecmeet = 0
replace hhoilsecmeet = 1 if household_1 == 1 | household_2 == 1 | ///
	household_3 == 1 | household_4 == 1 | household_5 == 1 | ///
	household_6 == 1 | household_7 == 1 | household_8 == 1 | ///
	household_9 == 1 | household_10 == 1 | household_11 == 1
replace hhoilsecmeet = . if hhaction == .
	
gen hhcsopart = 0
replace hhcsopart = 1 if household_1 == 2 | household_2 == 2 | ///
	household_3 == 2 | household_4 == 2 | household_5 == 2 | ///
	household_6 == 2 | household_7 == 2 | household_8 == 2 | ///
	household_9 == 2 | household_10 == 2 | household_11 == 2
replace hhcsopart =. if hhaction ==.

gen hhprot = 0
replace hhprot = 1 if household_1 == 3 | household_2 == 3 | ///
	household_3 == 3 | household_4 == 3 | household_5 == 3 | ///
	household_6 == 3 | household_7 == 3 | household_8 == 3 | ///
	household_9 == 3 | household_10 == 3 | household_11 == 3
replace hhprot = . if hhaction == .

gen hhvilllead = 0
replace hhvilllead = 1 if household_1 == 5 | household_2 == 5 | ///
	household_3 == 5 | household_4 == 5 | household_5 == 5 | ///
	household_6 == 5 | household_7 == 5 | household_8 == 5 | ///
	household_9 == 5 | household_10 == 5 | household_11 == 5
replace hhvilllead = . if hhaction == . 

gen hhvote = 0
replace hhvote = 1 if household_1 == 4 | household_2 == 4 | ///
	household_3 == 4 | household_4 == 4 | household_5 == 4 | ///
	household_6 == 4 | household_7 == 4 | household_8 == 4 | ///
	household_9 == 4 | household_10 == 4 | household_11 == 4
replace hhvote = . if hhaction == . 

gen hhlc3 = 0
replace hhlc3 = 1 if household_1 == 6 | household_2 == 6 | ///
	household_3 == 6 | household_4 == 6 | household_5 == 6 | ///
	household_6 == 6 | household_7 == 6 | household_8 == 6 | ///
	household_9 == 6 | household_10 == 6 | household_11 == 6
replace hhlc3 = . if hhaction == . 

gen hhlc5 = 0
replace hhlc5 = 1 if household_1 == 7 | household_2 == 7 | ///
	household_3 == 7 | household_4 == 7 | household_5 == 7 | ///
	household_6 == 7 | household_7 == 7 | household_8 == 7 | ///
	household_9 == 7 | household_10 == 7 | household_11 == 7
replace hhlc5 = . if hhaction == . 

gen hhdcmeet = 0
replace hhdcmeet = 1 if household_1 == 8 | household_2 == 8 | ///
	household_3 == 8 | household_4 == 8 | household_5 == 8 | ///
	household_6 == 8 | household_7 == 8 | household_8 == 8 | ///
	household_9 == 8 | household_10 == 8 | household_11 == 8
replace hhdcmeet = . if hhaction == . 

gen hhpolice = 0
replace hhpolice = 1 if household_1 == 9 | household_2 == 9 | ///
	household_3 == 9 | household_4 == 9 | household_5 == 9 | ///
	household_6 == 9 | household_7 == 9 | household_8 == 9 | ///
	household_9 == 9 | household_10 == 9 | household_11 == 9
replace hhpolice = . if hhaction == . 

gen hhpetition = 0
replace hhpetition = 1 if household_1 == 10 | household_2 == 10 | ///
	household_3 == 10 | household_4 == 10 | household_5 == 10 | ///
	household_6 == 10 | household_7 == 10 | household_8 == 10 | ///
	household_9 == 10 | household_10 == 10 | household_11 == 10
replace hhpetition = . if hhaction == . 

gen hhcourts = 0
replace hhcourts = 1 if household_1 == 11 | household_2 == 11 | ///
	household_3 == 11 | household_4 == 11 | household_5 == 11 | ///
	household_6 == 11 | household_7 == 11 | household_8 == 11 | ///
	household_9 == 11 | household_10 == 11 | household_11 == 11
replace hhcourts = . if hhaction == . 

gen hhlobby = 0
replace hhlobby = 1 if household_1 == 12 | household_2 == 12 | ///
	household_3 == 12 | household_4 == 12 | household_5 == 12 | ///
	household_6 == 12 | household_7 == 12 | household_8 == 12 | ///
	household_9 == 12 | household_10 == 12 | household_11 == 12
replace hhlobby = . if hhaction == . 

label var hhaction "Household-Any Actions"
label var hhoilsecmeet "Household-Oil Sector Meetings"
label var hhcsopart "Household-Paricipate with CSOs"
label var hhprot "Household-Protests"
label var hhvilllead "Household-Village Meetings"
label var hhvote "Household-Voting"
label var hhlc3 "Household-Meet with LC3"
label var hhlc5 "Household-Meet with LC5"
label var hhdcmeet "Household-Meet with District Commissioner"
label var hhpolice "Household-Contact Police"
label var hhpetition "Household-Sign a Petition"
label var hhcourts "Household-Use the Courts"
label var hhlobby "Household-Engage in Lobbying"

*** community
gen commoilsecmeet = 0
replace commoilsecmeet = 1 if community_1 == 1 | community_2 == 1 | ///
	community_3 == 1 | community_4 == 1 | community_5 == 1 | ///
	community_6 == 1 | community_7 == 1 | community_8 == 1 | ///
	community_9 == 1 | community_10 == 1 | community_11 == 1
replace commoilsecmeet = . if commaction == .
	
gen commcsopart = 0
replace commcsopart = 1 if community_1 == 2 | community_2 == 2 | ///
	community_3 == 2 | community_4 == 2 | community_5 == 2 | ///
	community_6 == 2 | community_7 == 2 | community_8 == 2 | ///
	community_9 == 2 | community_10 == 2 | community_11 == 2
replace commcsopart =. if commaction ==.

gen commprot = 0
replace commprot = 1 if community_1 == 3 | community_2 == 3 | ///
	community_3 == 3 | community_4 == 3 | community_5 == 3 | ///
	community_6 == 3 | community_7 == 3 | community_8 == 3 | ///
	community_9 == 3 | community_10 == 3 | community_11 == 3
replace commprot = . if commaction == .

gen commvilllead = 0
replace commvilllead = 1 if community_1 == 5 | community_2 == 5 | ///
	community_3 == 5 | community_4 == 5 | community_5 == 5 | ///
	community_6 == 5 | community_7 == 5 | community_8 == 5 | ///
	community_9 == 5 | community_10 == 5 | community_11 == 5
replace commvilllead = . if commaction == . 

gen commvote = 0
replace commvote = 1 if community_1 == 4 | community_2 == 4 | ///
	community_3 == 4 | community_4 == 4 | community_5 == 4 | ///
	community_6 == 4 | community_7 == 4 | community_8 == 4 | ///
	community_9 == 4 | community_10 == 4 | community_11 == 4
replace commvote = . if commaction == . 

gen commlc3 = 0
replace commlc3 = 1 if community_1 == 6 | community_2 == 6 | ///
	community_3 == 6 | community_4 == 6 | community_5 == 6 | ///
	community_6 == 6 | community_7 == 6 | community_8 == 6 | ///
	community_9 == 6 | community_10 == 6 | community_11 == 6
replace commlc3 = . if commaction == . 

gen commlc5 = 0
replace commlc5 = 1 if community_1 == 7 | community_2 == 7 | ///
	community_3 == 7 | community_4 == 7 | community_5 == 7 | ///
	community_6 == 7 | community_7 == 7 | community_8 == 7 | ///
	community_9 == 7 | community_10 == 7 | community_11 == 7
replace commlc5 = . if commaction == . 

gen commdcmeet = 0
replace commdcmeet = 1 if community_1 == 8 | community_2 == 8 | ///
	community_3 == 8 | community_4 == 8 | community_5 == 8 | ///
	community_6 == 8 | community_7 == 8 | community_8 == 8 | ///
	community_9 == 8 | community_10 == 8 | community_11 == 8
replace commdcmeet = . if commaction == . 

gen commpolice = 0
replace commpolice = 1 if community_1 == 9 | community_2 == 9 | ///
	community_3 == 9 | community_4 == 9 | community_5 == 9 | ///
	community_6 == 9 | community_7 == 9 | community_8 == 9 | ///
	community_9 == 9 | community_10 == 9 | community_11 == 9
replace commpolice = . if commaction == . 

gen commpetition = 0
replace commpetition = 1 if community_1 == 10 | community_2 == 10 | ///
	community_3 == 10 | community_4 == 10 | community_5 == 10 | ///
	community_6 == 10 | community_7 == 10 | community_8 == 10 | ///
	community_9 == 10 | community_10 == 10 | community_11 == 10
replace commpetition = . if commaction == . 

gen commcourts = 0
replace commcourts = 1 if community_1 == 11 | community_2 == 11 | ///
	community_3 == 11 | community_4 == 11 | community_5 == 11 | ///
	community_6 == 11 | community_7 == 11 | community_8 == 11 | ///
	community_9 == 11 | community_10 == 11 | community_11 == 11
replace commcourts = . if commaction == . 

gen commlobby = 0
replace commlobby = 1 if community_1 == 12 | community_2 == 12 | ///
	community_3 == 12 | community_4 == 12 | community_5 == 12 | ///
	community_6 == 12 | community_7 == 12 | community_8 == 12 | ///
	community_9 == 12 | community_10 == 12 | community_11 == 12
replace commlobby = . if commaction == . 

label var commaction "Community-Any Actions"
label var commoilsecmeet "Community-Oil Sector Meetings"
label var commcsopart "Community-Paricipate with CSOs"
label var commprot "Community-Protests"
label var commvilllead "Community-Village Meetings"
label var commvote "Community-Voting"
label var commlc3 "Community-Meet with LC3"
label var commlc5 "Community-Meet with LC5"
label var commdcmeet "Community-Meet with District Commissioner"
label var commpolice "Community-Contact Police"
label var commpetition "Community-Sign a Petition"
label var commcourts "Community-Use the Courts"
label var commlobby "Community-Engage in Lobbying"

*** action indices
gen totalhhact = hhoilsecmeet + hhprot + hhcsopart + hhvilllead + ///
hhvote + hhlc3 + hhlc5 + hhdcmeet + hhpolice + hhpetition + hhcourts + ///
hhlobby

gen totalcommact = commoilsecmeet + commprot + commcsopart + commvilllead + ///
commvote + commlc3 + commlc5 + commdcmeet + commpolice + commpetition + commcourts + ///
commlobby

*** creating composite index of satisfaction
gen compissue= (issueranking_land*satisfaction_land + ///
issueranking_socialserv*satisfaction_socialserv + ///
issueranking_localecodev*satisfaction_localecodev)/10
label var compissue "Composite Index of Outcomes"

*defining a macro for the outcome measures
global indexlim pcaindex_trans totalhhact totalcommact compissue

********************************
*********** RESULTS ************
********************************

********************************
*********** table 1 ************
********************************

* need to trick estout into stacking all did estimates from a single model
capt prog drop mergemodels
prog mergemodels, eclass
// assuming that last element in e(b)/e(V) is _cons
 version 8
 syntax namelist
 tempname b V tmp
 foreach name of local namelist {
   qui est restore `name'
   mat `b' = nullmat(`b') , e(b)
   mat `b' = `b'[1,1..colsof(`b')-1]
   mat `tmp' = e(V)
   mat `tmp' = `tmp'[1..rowsof(`tmp')-1,1..colsof(`tmp')-1]
   capt confirm matrix `V'
   if _rc {
     mat `V' = `tmp'
   }
   else {
     mat `V' = ///
      ( `V' , J(rowsof(`V'),colsof(`tmp'),0) ) \ ///
      ( J(rowsof(`tmp'),colsof(`V'),0) , `tmp' )
   }
 }
 local names: colfullnames `b'
 mat coln `V' = `names'
 mat rown `V' = `names'
 eret post `b' `V'
 eret local cmd "whatever"
end

* summary stats
qui eststo base: estpost ttest $indexlim if endline==0, by(treat)
qui eststo end: estpost ttest $indexlim if endline==1, by(treat)
estout base end using figures\sumstats1.csv, cells("mu_1(fmt(3)) mu_2(fmt(3))" "N_1(par([n= ]) fmt(0)) N_2(par([n= ]) fmt(0))") ///
	label varwidth(35) wrap replace

qui eststo cntrlbase: estpost summarize $indexlim if endline==0 & treat==0
qui eststo cntrlend: estpost summarize $indexlim if endline==1 & treat==0
qui eststo treatbase: estpost summarize $indexlim if endline==0 & treat==1
qui eststo treatend: estpost summarize $indexlim if endline==1 & treat==1
estout cntrlbase cntrlend treatbase treatend using figures\sumstats2.csv, ///
cells("sd (fmt(3))") varwidth(35) wrap replace

*** DID no clustering no FE

gen did=treat
foreach y in $indexlim {
	reg `y' did treat endline if endline==0, 
	estimates store `y'
}
mergemodels $indexlim
estimates store basediff

foreach y in $indexlim {
	reg `y' did treat endline if endline==1, 
	estimates store `y'
}
mergemodels $indexlim
estimates store enddiff

replace did=treat*endline
foreach y in $indexlim {
	reg `y' did treat endline,
	estimates store `y'
}
mergemodels $indexlim
estimates store diffndiff,

* DID table
estout basediff enddiff diffndiff using figures\baseDID.csv, ///
	cells( b(fmt(3) star) se(par fmt(3))) ///
	mlabels("Baseline" "Endline" "Diff-in-Diff") collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) keep(did) replace

*** DID village fixed effects

set matsize 10000

replace did=treat
foreach y in $indexlim {
	qui reg `y' did treat endline i.village if endline==0, 
	estimates store `y'
}
mergemodels $indexlim
estimates store basediff

foreach y in $indexlim {
	qui reg `y' did treat endline i.village if endline==1, 
	estimates store `y'
}
mergemodels $indexlim
estimates store enddiff

replace did=treat*endline
foreach y in $indexlim {
	qui reg `y' did treat endline i.village,
	estimates store `y'
}
mergemodels $indexlim
estimates store diffndiff,

estout basediff enddiff diffndiff using figures\FE_DID.csv, ///
	cells( b(fmt(3) star) se(par fmt(3))) ///
	mlabels("Baseline" "Endline" "Diff-in-Diff") collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) keep(did) ///
	prehead("With Village Fixed Effects") replace

*** DID village fixed effects and clustered errors

replace did=treat
foreach y in $indexlim {
	qui reg `y' did treat endline i.village if endline==0, cluster(village)
	estimates store `y'
}
mergemodels $indexlim
estimates store basediff

foreach y in $indexlim{
	qui reg `y' did treat endline i.village if endline==1, cluster(village)
	estimates store `y'
}
mergemodels $indexlim
estimates store enddiff

replace did=treat*endline
foreach y in $indexlim {
	qui reg `y' did treat endline i.village, cluster(village)
	estimates store `y'
}
mergemodels $indexlim
estimates store diffndiff,

estout basediff enddiff diffndiff using figures\FE_cluster_DID.csv, ///
	cells( b(fmt(3) star) se(par fmt(3))) ///
	mlabels("Baseline" "Endline" "Diff-in-Diff") collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) keep(did) ///
	prehead("With Village Fixed Effects and Clustered Errors") replace

********************************
******** SI Appendix ***********
********************************

********************************
******** outcome hist **********
********************************

hist pcaindex_trans, freq xtitle("Transparency Index")
graph export figures\SI\transindexhist.png, width(1000) replace

hist totalhhact, freq xtitle("Sum of household actions") ///
legend(off) saving(temp\ta1.gph, replace)
hist totalhhact if totalhhact !=0, freq xtitle("Sum of household actions") ///
xscale(range(1 10)) xlabel(1[1]10) saving(temp\ta2.gph, replace) legend(off)
graph combine temp\ta1.gph temp\ta2.gph, xsize(6) ysize(3) 
graph export figures\SI\hhacthist.png, width(1000) replace

hist totalcommact, freq xtitle("Sum of community actions") ///
legend(off) saving(temp\ta1.gph, replace)
hist totalcommact if totalcommact !=0, freq xtitle("Sum of community actions") ///
xscale(range(1 10)) xlabel(1[1]10) saving(temp\ta2.gph, replace) legend(off)
graph combine temp\ta1.gph temp\ta2.gph, xsize(6) ysize(3) 
graph export figures\SI\commacthist.png, width(2000) replace

hist compissue, freq xtitle("Satisfaction Index")
graph export figures\SI\satisindexhist.png, width(1000) replace

********************************
********** SI table 1 **********
********************************

* DID estimates net satisfaction and importance

global netimpsat issueranking_land issueranking_socialserv issueranking_localecodev ///
satisfaction_land satisfaction_socialserv satisfaction_localecodev 

* whole sample

replace did = treat

foreach y in $netimpsat {
	qui reg `y' did treat endline if endline==0, cluster(village)
	estimates store `y'
}
mergemodels $netimpsat
estimates store basediff

foreach y in $netimpsat {
	qui reg `y' did treat endline if endline==1, cluster(village)
	estimates store `y'
}
mergemodels $netimpsat
estimates store enddiff

replace did = treat * endline
foreach y in $netimpsat {
	qui reg `y' did treat endline, cluster(village)
	estimates store `y'
}
mergemodels $netimpsat
estimates store diffndiff,

estout basediff enddiff diffndiff using figures\SI\SI_DID_impsat.csv, ///
	cells( b(fmt(3) star) se(par fmt(3))) ///
	mlabels("Baseline" "Endline" "Diff-in-Diff") collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) keep(did) ///
	prehead("Net importance and satisfaction") replace

* men

replace did = treat

foreach y in $netimpsat {
	qui reg `y' did treat endline if endline==0 & gender == 0, cluster(village)
	estimates store `y'
}
mergemodels $netimpsat
estimates store basediff

foreach y in $netimpsat {
	qui reg `y' did treat endline if endline==1 & gender == 0, cluster(village)
	estimates store `y'
}
mergemodels $netimpsat
estimates store enddiff

replace did = treat * endline
foreach y in $netimpsat {
	qui reg `y' did treat endline if gender == 0, cluster(village)
	estimates store `y'
}
mergemodels $netimpsat
estimates store diffndiff,

estout basediff enddiff diffndiff using figures\SI\SI_DID_impsat_men.csv, ///
	cells( b(fmt(3) star) se(par fmt(3))) ///
	mlabels("Baseline" "Endline" "Diff-in-Diff") collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) keep(did) ///
	prehead("Net importance and satisfaction, Men") replace

* women

replace did = treat

foreach y in $netimpsat {
	qui reg `y' did treat endline if endline==0 & gender == 1, cluster(village)
	estimates store `y'
}
mergemodels $netimpsat
estimates store basediff

foreach y in $netimpsat {
	qui reg `y' did treat endline if endline==1 & gender == 1, cluster(village)
	estimates store `y'
}
mergemodels $netimpsat
estimates store enddiff

replace did = treat * endline
foreach y in $netimpsat {
	qui reg `y' did treat endline if gender == 1, cluster(village)
	estimates store `y'
}
mergemodels $netimpsat
estimates store diffndiff,

estout basediff enddiff diffndiff using figures\SI\SI_DID_impsat_women.csv, ///
	cells( b(fmt(3) star) se(par fmt(3))) ///
	mlabels("Baseline" "Endline" "Diff-in-Diff") collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) keep(did) ///
	prehead("Net importance and satisfaction, Women") replace

********************************
******** balance tests *********
********************************

*continuous variables
global cont issueranking_land issueranking_socialserv issueranking_localecodev satisfaction_land satisfaction_socialserv satisfaction_localecodev blame_comm blame_vill blame_subc blame_dist blame_oil blame_cent blame_civi credit_comm credit_vill credit_subc credit_dist credit_oil credit_cent credit_civi tftransp totalcommact totalhhact efficacy accountability reside school hhsize
qui estpost ttest $cont, by(treat)
estout , cells("mu_1(fmt(%9.3f) label(Control)) mu_2(fmt(%9.3f) label(Treatment)) b(star fmt(%9.3f) label(Difference)) p(par fmt(%9.3f) label(p-value)) count(fmt(%9.0f) label(Obs))") ///
	label varwidth(75) prehead("Difference of means t-tests") 

*categorical variables
global cat ownland claimland busassoc skillstrain directemploy indirectemploy psclacc ssclacc hcacc swacc rbacc mktacc vocpacc elecacc indcomminter commoilinter commlocgovtinter commcengovtinter commcsointer infosatisfy helpsatisfy 
qui estpost prtest $cat, by(treat)
estout , cells("P_1(fmt(%9.3f) label(Control)) P_2(fmt(%9.3f) label(Treatment)) b(star fmt(%9.3f) label(Difference)) p(par fmt(%9.3f) label(p-value)) count(fmt(%9.0f) label(Obs))") ///
	label varwidth(75) prehead("Difference of proportions z-tests")	

qui estpost sum $cat if treat==1
matrix trme=e(mean)'
mat list trme
svmat trme
rename trme treatmean

qui estpost sum $cat if treat==0
matrix trme=e(mean)'
mat list trme
svmat trme
rename trme contmean

gen varnam=_n if _n<22
label define vars 1 "Own Land" 2 "Claim on Land" 3 "Member of Bus. Assoc." 4 "Skills Training" 5 "Oil Industry Job" 6 "Support Job" 7 "Primary School Access" 8 "Secondary School Access" 9 "Health Clinic Access" 10 "Safe Water Access" 11 "Road Access" 12 "Market Access" 13 "Vocational Program Access" 14 "Electricty Access" 15 "Ind-Community Interactions" 16 "Community-Oil Interactions" 17 "Community-Local Govt Interactions" 18 "Community-Central Govt Interactions" 19 "Community-CSO Interactions" 20 "Information Access" 21 "Face Problems"		
label values varnam vars
label var treatmean "Treatment"
label var contmean "Control"

twoway (scatter varnam treatmean, mc(gs3) ms(Th)) (scatter varnam contmean, mc(gs3) ms(Oh)), ///
	yline(1/21, lcol(gs13)) ylab(1/21, notick valuelabel angle(horizontal) labsize(small)) ///
	ytitle("") legend(order(1 "Treatment" 2 "Control") cols(1) ring(0) position(1) bmargin(0 -3 0 10)) ///
	xsize(5) ysize(7) title("Proportions", span) subtitle("Binary Variables", span) ysc(noline)
graph export figures\SI\binaryvars.png, replace
drop treatmean contmean varnam

qui estpost sum $cont if treat==1, de
matrix trme=(e(p50)',e(p25)',e(p75)',e(max)',e(min)')
mat list trme
svmat trme
rename trme1 treatmed
rename trme2 treatp25
rename trme3 treatp75
rename trme4 treatmax
rename trme5 treatmin
* these are to get the upper and lower adjacent values (1.5* interquartile range)
gen treatul=treatp75+1.5*(treatp75-treatp25)
replace treatul=min(treatul,treatmax)
gen treatll=treatp25-1.5*(treatp75-treatp25)
replace treatll=max(treatll,treatmin)
*outliers
matrix treatoutuls=(.,.)
matrix treatoutlls=(.,.)
foreach x in $cont {
	local i=`i'+1 // counter to keep track of variables
	
	mat counts=.
	mat values=.
	mat outuls=(.,.)
	qui sum `x' if treat==1, de // uppper limit outliers
	tab `x' if (`x' > r(p75)+1.5*(r(p75)-r(p25))) & (treat==1), matcell(counts) matrow(values)
	mat outuls=(counts, values)
	mat rownames outuls=`i'
	if `i'==1 matrix treatoutuls=outuls
		else matrix treatoutuls=(treatoutuls \ outuls) 
	
	mat counts=.
	mat values=.
	mat outlls=(.,.)
	qui sum `x' if treat==1, de // lower limit outliers
	tab `x' if (`x' < r(p25)-1.5*(r(p75)-r(p25))) & (treat==1), matcell(counts) matrow(values)
	mat outlls=(counts, values)
	mat rownames outlls=`i'
	if `i'==1 matrix treatoutlls=outlls
		else matrix treatoutlls=(treatoutlls \ outlls) 
}

svmat2 treatoutuls, names(treatoutuls_count treatoutuls_values) rnames(treatoutuls_varnam)
destring treatoutuls_varnam, replace
replace treatoutuls_varnam=treatoutuls_varnam+.2

svmat2 treatoutlls, names(treatoutlls_count treatoutlls_values) rnames(treatoutlls_varnam)
destring treatoutlls_varnam, replace
replace treatoutlls_varnam=treatoutlls_varnam+.2

* get distribuitonal data for treatment group for boxplots
qui estpost sum $cont if treat==0, de
matrix trme=(e(p50)',e(p25)',e(p75)',e(max)',e(min)')
mat list trme
svmat trme
rename trme1 contmed
rename trme2 contp25
rename trme3 contp75
rename trme4 contmax
rename trme5 contmin
* these are to get the upper and lower adjacent values (1.5* interquartile range)
gen contul=contp75+1.5*(contp75-contp25)
replace contul=min(contul,contmax)
gen contll=contp25-1.5*(contp75-contp25)
replace contll=max(contll,contmin)
*outliers
mat contoutuls=(.,.)
mat contoutlls=(.,.)
foreach x in $cont {
	local j=`j'+1 // counter to keep track of variables
	
	mat counts=.
	mat values=.
	mat outuls=(.,.)
	qui sum `x' if treat==0, de // uppper limit outliers
	tab `x' if (`x' > r(p75)+1.5*(r(p75)-r(p25))) & (treat==0), matcell(counts) matrow(values)
	mat outuls=(counts, values)
	mat rownames outuls=`j'
	if `j'==1 matrix contoutuls=outuls
		else matrix contoutuls=(contoutuls \ outuls) 
	
	mat counts=.
	mat values=.
	mat outlls=(.,.)
	qui sum `x' if treat==0, de // lower limit outliers
	tab `x' if (`x' < r(p25)-1.5*(r(p75)-r(p25))) & (treat==0), matcell(counts) matrow(values)
	mat outlls=(counts, values)
	mat rownames outlls=`j'
	if `j'==1 matrix contoutlls=outlls
		else matrix contoutlls=(contoutlls \ outlls) 
}

svmat2 contoutuls, names(contoutuls_count contoutuls_values) rnames(contoutuls_varnam)
destring contoutuls_varnam, replace
replace contoutuls_varnam=contoutuls_varnam-.2

svmat2 contoutlls, names(contoutlls_count contoutlls_values) rnames(contoutlls_varnam)
destring contoutlls_varnam, replace
replace contoutlls_varnam=contoutlls_varnam-.2

* now to make the figure
gen varnam=_n if _n<31
gen varnamcont=varnam-.2
gen varnamtreat=varnam+.2
label define vars1 1 "Issue Rank: Land" 2 "Issue Rank: Social Services" 3 "Issue Rank: Local Econ Dev" 4 "Satisfaction Rank: Land" 5 "Satisfaction Rank: Social Services" 6 "Satisfaction Rank: Local Econ Dev" 7 "Blame: Community Itself" 8 "Blame Village Leader" 9 "Blame: Subcounty Leader" 10 "Blame: District Leader" 11 "Blame: Oil Company" 12 "Blame: Centeral Govt" 13 "Blame: CSOs" 14 "Credit: Community Itself" 15 "Credit Village Leader" 16 "Credit: Subcounty Leader" 17 "Credit: District Leader" 18 "Credit: Oil Company" 19 "Credit: Centeral Govt" 20 "Credit: CSOs" 21 "Oil Knowledge" 22 "Community Actions" 23 "Household Actions" 24 "CSO Actions" 25 "No. of CSOs" 26 "Efficacy" 27 "Accountability" 28 "Reside" 29 "School" 30 "Household Size" 		
foreach x in varnamcont varnamtreat varnam treatoutlls_varnam treatoutuls_varnam contoutlls_varnam contoutuls_varnam {
	label values `x' vars1
	}
label var treatmed "Treatment"
label var contmed "Control"

twoway 	(scatter treatoutuls_varnam treatoutuls_values, ms(o) msize(small) mc(gs13)) ///
	(scatter treatoutlls_varnam treatoutlls_values, ms(o) msize(small) mc(gs13)) /// 
	(scatter contoutuls_varnam contoutuls_values, ms(o) msize(small) mc(gs13)) ///
	(scatter contoutlls_varnam contoutlls_values, ms(o) msize(small) mc(gs13)) ///
	(rcap treatul treatll varnamtreat, horizontal msize(vsmall) lc(gs5)) ///
	(rcap contul contll varnamcont, horizontal msize(vsmall) lc(gs5)) ///
	(rbar treatp25 treatp75 varnamtreat, horizontal barwidth(.3) lc(gs3) fc(gs3)) ///
	(rbar contp25 contp75 varnamcont, horizontal barwidth(.3) lc(gs3) fc(gs13)) ///
	(rcap treatmed treatmed varnamtreat, horizontal lc(gs0) msize(small)) ///
	(rcap contmed contmed varnamcont, horizontal lc(gs0) msize(small)), ///
	yline(1/30, lcol(gs13)) ylab(1/30, notick valuelabel angle(horizontal) labsize(vsmall)) ///
	ytitle("") legend(order(7 "Treatment" 8 "Control") symx(*.3) keygap(*.3) cols(1) ring(0) position(1)  bmargin(0 -3 0 10)) ///
	xsize(4) ysize(7) title("Box and Whisker Plots", span) subtitle("Continuous Variables", span) ysc(noline)
graph export figures\SI\contvars.png, replace

drop treatmed treatp25 treatp75 treatmax treatmin treatul treatll treatoutuls_count treatoutuls_values treatoutuls_varnam treatoutlls_count treatoutlls_values treatoutlls_varnam contmed contp25 contp75 contmax contmin contul contll contoutuls_count contoutuls_values contoutuls_varnam contoutlls_count contoutlls_values contoutlls_varnam varnam varnamcont varnamtreat

*****************************
******** Robustness *********
*****************************

*visits figure

gen freqvin = 0
replace freqvin = 1 if visitin == 1 | visitin == 2
gen freqvout = 0
replace freqvout = 1 if visitout == 1 | visitout == 2

graph bar (mean) freqvin freqvout if endline == 0, ///
over(DISTRICT) ///
title("") ytitle("") legend(off) ///
legend(label(1 "Frequent visits in") label(2 "Frequent visits out")) ///
saving(temp\bhist1.gph, replace)
graph export figures\SI\visitfreq.png, width(1000) replace

*** DID estimates for rubstness tests

*Spillovers
replace did = treat * endline
gen weight_0=1

foreach i in 0 1 3 5 10 {
	foreach y in $indexlim {
		qui reg `y' did treat endline [pw=weight_`i'], cluster(village)
		estimates store `y'
	}
	mergemodels $indexlim
	estimates store diffndiff`i'
}
estout diffndiff0 diffndiff1 diffndiff3 diffndiff5 diffndiff10 using figures\SI\SI_DID_spill.csv, ///
	cells( b(fmt(3) star) se(par fmt(3))) ///
	mlabels("None" "1 km" "3 km" "5km" "10km") collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) keep(did) ///
	prehead("Spillover Assumptions - DID estimates") replace

*forums
gen g1 = 0
replace g1 = 1 if DISTRICT == "Buliisa" | ///
DISTRICT == "Hoima" | DISTRICT == "Ntoroko"

gen g2 = 0
replace g2 = 1 if DISTRICT == "Arua" | ///
DISTRICT == "Moyo" | DISTRICT == "Nebbi" | ///
DISTRICT == "Nwoya"

replace did=treat
set matsize 1000

foreach y in $indexlim {
	qui reg `y' did treat endline g1 g2 if endline==0, cluster(village)
	estimates store `y'
}
mergemodels $indexlim
estimates store basediff

foreach y in $indexlim {
	qui reg `y' did treat endline g1 g2 if endline==1, cluster(village)
	estimates store `y'
}
mergemodels $indexlim
estimates store enddiff

replace did=treat*endline
foreach y in $indexlim {
	qui reg `y' did treat endline g1 g2, cluster(village)
	estimates store `y'
}
mergemodels $indexlim
estimates store diffndiff,

estout basediff enddiff diffndiff using figures\SI\SI_DID_forum.csv, ///
	cells( b(fmt(3) star) se(par fmt(3))) ///
	mlabels("Baseline" "Endline" "Diff-in-Diff") collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) keep(did) ///
	prehead("Forum groups") replace

* refugee camps

replace did=treat
set matsize 1000

foreach y in $indexlim {
	qui reg `y' did treat endline distcamp if endline==0, cluster(village)
	estimates store `y'
}
mergemodels $indexlim
estimates store basediff

foreach y in $indexlim {
	qui reg `y' did treat endline distcamp if endline==1, cluster(village)
	estimates store `y'
}
mergemodels $indexlim
estimates store enddiff

replace did=treat*endline

foreach y in $indexlim {
	qui reg `y' did treat endline distcamp, cluster(village)
	estimates store `y'
}
mergemodels $indexlim
estimates store diffndiff,

estout basediff enddiff diffndiff using figures\SI\SI_DID_refugee.csv, ///
	cells( b(fmt(3) star) se(par fmt(3))) ///
	mlabels("Baseline" "Endline" "Diff-in-Diff") collabels(none) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) keep(did) ///
	prehead("Household distance to refugee camps") replace



